package com.boc.ewr.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.BusAcctInfo;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.HadAcctD2;
import com.boc.ewr.bcimp.service.IBusAcctInfoService;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.IHadAcctD2Service;
import com.boc.ewr.business.entity.AcctDtlClassD;
import com.boc.ewr.business.mapper.AcctDtlClassDMapper;
import com.boc.ewr.business.service.IAcctDtlClassDService;
import com.boc.ewr.util.file.enumeration.YnEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: D类账户明细
 * @Author: jeecg-boot
 * @Date:   2023-05-26
 * @Version: V1.0
 */
@Service
public class AcctDtlClassDServiceImpl extends ServiceImpl<AcctDtlClassDMapper, AcctDtlClassD> implements IAcctDtlClassDService {

    @Autowired
    private IHadAcctD2Service hadAcctD2Service; //D2类列表

    @Autowired
    private ICacService cacService; //CAC

    @Autowired
    private IBusAcctInfoService busAcctInfoService; //对公账户信息

    @Autowired
    AcctDtlClassDMapper acctDtlClassDMapper;

    //产生数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createInfo() {

        //saveBatch(baseMapper.creatInfo());
        //查询D1、D3类型的账户
        Set<AcctDtlClassD> result = new HashSet<>(acctDtlClassDMapper.creatInfo());
        //查询D2类
        List<HadAcctD2> d2List = hadAcctD2Service.list();
        List<String> acctNoList = d2List.stream().map(HadAcctD2::getAcctNo).collect(Collectors.toList());

        //需要和对公账户信息、CAC关联插入D类账户表
        List<BusAcctInfo> acctDList = busAcctInfoService.list(
                new LambdaQueryWrapper<BusAcctInfo>().in(BusAcctInfo::getAccNo, acctNoList)
        );
        List<Cac> cacList = cacService.list(
                new LambdaQueryWrapper<Cac>().in(Cac::getAccNo, acctNoList)
        );
        //根据账号，将数据组装出来
        for(HadAcctD2 acctD2 : d2List){
            AcctDtlClassD res = new AcctDtlClassD();
            BusAcctInfo acctTemp = acctDList.stream().filter(acct -> acctD2.getAcctNo().equals(acct.getAccNo())).collect(Collectors.toList()).get(0);
            Cac cacTemp = cacList.stream().filter(cac -> acctD2.getAcctNo().equals(cac.getAccNo())).collect(Collectors.toList()).get(0);
            BeanUtils.copyProperties(acctTemp, res);
            res.setCaNo(cacTemp.getCaNo());
            res.setProtocolNo(cacTemp.getCaProtocolNo());
            res.setProtocolType("1");//D2类导入算是
            res.setCurrBal(cacTemp.getCurrBal());
            res.setCaPeriodNo(cacTemp.getCaPeriodNo());
            res.setCreateTime(new Date());
            res.setIsClassD2(YnEnum.YES.getIndex());
            result.add(res);
        }
        saveBatch(result);

    }
}
